#include "selfinfowidget.h"
#include "model/datacenter.h"
#include "model/data.h"

SelfInfoWidget::SelfInfoWidget(QWidget *parent) : QDialog(parent)
{
    //基本设置
    this->setWindowTitle("个人信息");
    this->setFixedSize(450,300);
    this->setAttribute(Qt::WA_DeleteOnClose);
    this->move(QCursor::pos());
    this->setStyleSheet("QDialog{background-color:rgb(255,255,255);}");

    //创建水平布局管理器
    QHBoxLayout* hlayout = new QHBoxLayout(this);
    hlayout->setSpacing(10);
    hlayout->setContentsMargins(20,20,20,20);
    //这里一定要设置紧凑布局,方便之后添加验证码新行
    hlayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
    this->setLayout(hlayout);

    //创建头像
    avatar = new QPushButton(this);
    avatar->setFixedSize(60,60);
    avatar->setIconSize(QSize(60,60));
    avatar->setStyleSheet("QPushButton{border:none;}");

    hlayout->addWidget(avatar,0,Qt::AlignTop);

    //创建网格布局管理器
    layout = new QGridLayout(this);
    layout->setSpacing(10);
    layout->setContentsMargins(0,0,0,0);
    hlayout->addLayout(layout);

    QSize sizeBtn(20,20);
    QSize sizeEdit(200,30); //显示区域或输入框长度

    //用户id

        QLabel* idTag = new QLabel(this);
        idTag->setText("序号");
        idTag->setStyleSheet("QLabel { font-weight: 800; }");
        idLabel = new QLabel(this);
        idLabel->setFixedSize(sizeEdit);

    //用户昵称

        QLabel* nameTag = new QLabel(this);
        nameTag->setText("昵称");
        nameTag->setStyleSheet("QLabel { font-weight: 800; }");
        nameLabel = new QLabel(this);
        nameLabel->setFixedSize(sizeEdit);
        nameEdit = new QLineEdit(this);
        nameEdit->setFixedSize(sizeEdit);

        nameModifyBtn = new QPushButton(this);
        nameModifyBtn->setIcon(QIcon(":/image/modify.png"));
        nameSubmitBtn = new QPushButton(this);
        nameSubmitBtn->setIcon(QIcon(":/image/submit.png"));

        nameEdit->hide();
        nameSubmitBtn->hide();


    //用户签名

        QLabel* descTag = new QLabel(this);
        descTag->setText("签名");
        descTag->setStyleSheet("QLabel { font-weight: 800; }");
        descLabel = new QLabel(this);
        descLabel->setFixedSize(sizeEdit);
        descEdit = new QLineEdit(this);
        descEdit->setFixedSize(sizeEdit);

        descModifyBtn = new QPushButton(this);
        descModifyBtn->setIcon(QIcon(":/image/modify.png"));
        descSubmitBtn = new QPushButton(this);
        descSubmitBtn->setIcon(QIcon(":/image/submit.png"));

        descEdit->hide();
        descSubmitBtn->hide();


    //用户电话

        QLabel* phoneTag = new QLabel(this);
        phoneTag->setText("电话");
        phoneTag->setStyleSheet("QLabel { font-weight: 800; }");
        phoneLabel = new QLabel(this);
        phoneLabel->setFixedSize(sizeEdit);
        phoneEdit = new QLineEdit(this);
        phoneEdit->setFixedSize(sizeEdit);

        phoneModifyBtn = new QPushButton(this);
        phoneModifyBtn->setIcon(QIcon(":/image/modify.png"));
        phoneSubmitBtn = new QPushButton(this);
        phoneSubmitBtn->setIcon(QIcon(":/image/submit.png"));

        phoneEdit->hide();
        phoneSubmitBtn->hide();


    //电话号码:验证码逻辑
        verityTag = new QLabel(this);
        verityTag->setText("验证码");
        verityCodeEdit = new QLineEdit(this);
        verityCodeEdit->setFixedSize(sizeEdit);
        sendVerityCodeBtn = new QPushButton(this);
        sendVerityCodeBtn->setText("获取验证码");

        verityTag->hide();
        verityCodeEdit->hide();
        sendVerityCodeBtn->hide();

    //设置布局

        //设置tag
        layout->addWidget(idTag,0,0);
        layout->addWidget(nameTag,1,0);
        layout->addWidget(descTag,2,0);
        layout->addWidget(phoneTag,3,0);


        //设置文本框/输入框
        layout->addWidget(idLabel,0,1);
        layout->addWidget(nameLabel,1,1);
        layout->addWidget(descLabel,2,1);
        layout->addWidget(phoneLabel,3,1);

        //设置按钮
        layout->addWidget(nameModifyBtn,1,2);
        layout->addWidget(descModifyBtn,2,2);
        layout->addWidget(phoneModifyBtn,3,2);

        //设置拉伸因子
        layout->setRowStretch(0, 1);
        layout->setRowStretch(1, 1);
        layout->setRowStretch(2, 1);
        layout->setRowStretch(3, 1);
        layout->setRowStretch(4, 1);

        //设置第一行的拉伸策略为 "被动伸缩" QSizePolicy::Preferred , 与 "主动延伸" QSizePolicy::Expending 不同, 其会默认使用提供的尺寸大小, 而非有空间就延伸
        QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
        idTag->setSizePolicy(sizePolicy);
        idLabel->setSizePolicy(sizePolicy);

        //初始化槽函数
        initSignalSlot();

#ifdef test

    avatar->setIcon(QIcon(":/image/logo.png"));
    idLabel->setText("0001");
    nameLabel->setText("张三");
    descLabel->setText("descLabel测试签名");
    phoneLabel->setText("phoneLabel测试电话");
#endif

    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    model::UserInfo* myselfInfo = dataCenter->getMyself();

    if(myselfInfo == nullptr)
    {
        LOG() << "加载个人信息失败: dataCenter保存的自我用户信息为空!";
        return;
    }
    else
    {
        avatar->setIcon(myselfInfo->userIcon);
        idLabel->setText(myselfInfo->userId);
        nameLabel->setText(myselfInfo->userName);
        descLabel->setText(myselfInfo->userSignature);
        phoneLabel->setText(myselfInfo->userPhone);
    }



}
void SelfInfoWidget::initSignalSlot()
{
    ////////////////////////////////////////////////////////////////
    /// 页面样式随按钮切换逻辑
    /// /////////////////////////////////////////////////////////////
    connect(nameModifyBtn,&QPushButton::clicked,this,&SelfInfoWidget::clickNameModifyBtn);
    connect(nameSubmitBtn,&QPushButton::clicked,this,&SelfInfoWidget::clickNameSubmitBtn);

    connect(descModifyBtn,&QPushButton::clicked,this,&SelfInfoWidget::clickDescModifyBtn);
    connect(descSubmitBtn,&QPushButton::clicked,this,&SelfInfoWidget::clickDescSubmitBtn);

    connect(phoneModifyBtn,&QPushButton::clicked,this,&SelfInfoWidget::clickPhoneModifyBtn);
    connect(phoneSubmitBtn,&QPushButton::clicked,this,&SelfInfoWidget::clickPhoneSubmitBtn);

    connect(sendVerityCodeBtn,&QPushButton::clicked,this,&SelfInfoWidget::clickGetVerifyCodeBtn);
    connect(avatar,&QPushButton::clicked,this,&SelfInfoWidget::clickAvatarBtn);

}
///////////////////////////////////////////////////////////////////
/// \brief SelfInfoWidget::clickNameModifyBtn
///
void SelfInfoWidget::clickNameModifyBtn()
{
    //隐藏旧
    nameLabel->hide();
    nameModifyBtn->hide();
    layout->removeWidget(nameLabel);
    layout->removeWidget(nameModifyBtn);
    //显示新
    nameEdit->show();
    nameSubmitBtn->show();
    layout->addWidget(nameEdit,1,1);
    layout->addWidget(nameSubmitBtn,1,2);
    //清除原有昵称文本
    nameEdit->setText("");
}

void SelfInfoWidget::clickNameSubmitBtn()
{
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    QString text = nameEdit->text();
    connect(dataCenter,&model::DataCenter::changeNicknameDone,this, &SelfInfoWidget::clickNameSubmitBtnDone,Qt::UniqueConnection);
    dataCenter->changeNicknameAsync(text);
}

void SelfInfoWidget::clickNameSubmitBtnDone()
{
    //隐藏旧
    nameEdit->hide();
    nameSubmitBtn->hide();
    layout->removeWidget(nameEdit);
    layout->removeWidget(nameSubmitBtn);

    //显示新
    nameLabel->show();
    nameModifyBtn->show();
    layout->addWidget(nameLabel,1,1);
    layout->addWidget(nameModifyBtn,1,2);
    //设置昵称文本
    nameLabel->setText(nameEdit->text());
}
/////////////////////////////////////////////////////////////////////////
/// \brief SelfInfoWidget::clickDescModifyBtn
///
void SelfInfoWidget::clickDescModifyBtn()
{
    //隐藏旧
    descLabel->hide();
    descModifyBtn->hide();
    layout->removeWidget(descLabel);
    layout->removeWidget(descModifyBtn);
    //显示新
    descEdit->show();
    descSubmitBtn->show();
    layout->addWidget(descEdit,2,1);
    layout->addWidget(descSubmitBtn,2,2);
    //清除原有签名文本
    descEdit->setText("");

}

void SelfInfoWidget::clickDescSubmitBtn()
{
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    QString text = descEdit->text();
    connect(dataCenter,&model::DataCenter::changeDescriptionDone,this, &SelfInfoWidget::clickDescSubmitBtnDone,Qt::UniqueConnection);
    dataCenter->changeDescriptionAsync(text);
}

void SelfInfoWidget::clickDescSubmitBtnDone()
{
    //隐藏旧
    descEdit->hide();
    descSubmitBtn->hide();
    layout->removeWidget(descEdit);
    layout->removeWidget(descSubmitBtn);

    //显示新
    descLabel->show();
    descModifyBtn->show();
    layout->addWidget(descLabel,2,1);
    layout->addWidget(descModifyBtn,2,2);
    //设置签名文本
    descLabel->setText(descEdit->text());
}
/////////////////////////////////////////////////////////////////////////
/// \brief SelfInfoWidget::clickGetVerifyCodeBtn
///
void SelfInfoWidget::clickGetVerifyCodeBtn()
{
    //获取输入框内容
    const QString& phone = phoneEdit->text();
    if(phone.isEmpty())
    {
        LOG() << "SelfInfoWidget::clickGetVerifyCodeBtn 错误! 电话号码不能为空!";
        return;
    }

    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    connect(dataCenter,&model::DataCenter::getVerifyCodeDone,this,[=](){
        Toast::showMessage("短信验证码已经发送, 请即时查收");
    });
    dataCenter->getVerifyCodeAsync(phone);

    this->phoneNumber = phone;

    //禁用发送按钮
    sendVerityCodeBtn->setEnabled(false);

    //启动定时器, 在一段时间后将按钮重设为可用状态
    verityTime = 30;
    QTimer* timer = new QTimer(this);

    connect(timer,&QTimer::timeout,this,[=](){
        if(verityTime >= 1)
        {
            --verityTime;
            sendVerityCodeBtn->setText(QString::number(verityTime) + "s");
        }
        else
        {
            sendVerityCodeBtn->setText("获取验证码");
            sendVerityCodeBtn->setEnabled(true);
            timer->stop();
            timer->deleteLater();
            return;
        }
    });

    timer->start(1000);
}
/////////////////////////////////////////////////////////////////////////
/// \brief SelfInfoWidget::clickPhoneModifyBtn
///
void SelfInfoWidget::clickPhoneModifyBtn()
{
    //隐藏旧
    phoneLabel->hide();
    phoneModifyBtn->hide();
    layout->removeWidget(phoneLabel);
    layout->removeWidget(phoneModifyBtn);

    //显示新
    phoneEdit->show();
    phoneSubmitBtn->show();
    layout->addWidget(phoneEdit,3,1);
    layout->addWidget(phoneSubmitBtn,3,2);

    //验证码显示
    verityTag->show();
    verityCodeEdit->show();
    sendVerityCodeBtn->show();
    layout->addWidget(verityTag,4,0);
    layout->addWidget(verityCodeEdit,4,1);
    layout->addWidget(sendVerityCodeBtn,4,2);


}

void SelfInfoWidget::clickPhoneSubmitBtn()
{
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    //获取验证码id
    QString verifyCodeId = dataCenter->getVerifyCodeId();
    if(verifyCodeId.isEmpty())
    {
        LOG() << "SelfInfoWidget::clickPhoneSubmitBtn 失败! 验证码尚未获取!";
        return;
    }

    //获取验证码,通过输入框获取
    QString verifyCode = verityCodeEdit->text();
    if(verifyCode.isEmpty())
    {
        LOG() << "SelfInfoWidget::clickPhoneSubmitBtn 失败! 用户未填写验证码!";
        return;
    }

    //通过网络发送请求
    connect(dataCenter,&model::DataCenter::changePhoneDone,this,&SelfInfoWidget::clickPhoneSubmitBtnDone,Qt::UniqueConnection);
    dataCenter->changePhoneAsync(this->phoneNumber,verifyCodeId,verifyCode);

    verityTime = 0;


}
void SelfInfoWidget::clickPhoneSubmitBtnDone()
{
    //隐藏旧
    phoneEdit->hide();
    phoneSubmitBtn->hide();
    layout->removeWidget(phoneEdit);
    layout->removeWidget(phoneSubmitBtn);

    //验证码隐藏
    verityTag->hide();
    verityCodeEdit->hide();
    sendVerityCodeBtn->hide();

    layout->removeWidget(verityTag);
    layout->removeWidget(verityCodeEdit);
    layout->removeWidget(sendVerityCodeBtn);

    //显示新
    phoneLabel->show();
    phoneModifyBtn->show();
    layout->addWidget(phoneLabel,3,1);
    layout->addWidget(phoneModifyBtn,3,2);

    //设置电话文本
    phoneLabel->setText(phoneEdit->text());

}

void SelfInfoWidget::clickAvatarBtn()
{
    QString filter = "Image files (*.png *.jpeg *.jpg *.bmp)";
    QString filePath = QFileDialog::getOpenFileName(this, "选择图片", QDir::homePath(),filter);
    if(filePath.isEmpty())
    {
        LOG() << "clickAvatarBtn 未选择图片!";
        return;
    }

    QByteArray avatarData = model::loadFileToByteArray(filePath);
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    connect(dataCenter,&model::DataCenter::changeAvatarDone,this,&SelfInfoWidget::clickAvatarBtnDone,Qt::UniqueConnection);
    dataCenter->changeAvatarAsync(avatarData);

}

void SelfInfoWidget::clickAvatarBtnDone()
{
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    avatar->setIcon(dataCenter->getMyself()->userIcon);
}







































